/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */
package com.Serverce;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.Client.ClientThread;


public final class Config {
	private static final Logger _log = Logger.getLogger(Config.class.getName());

	private static String[] _systemInfo;

	/** Server control */
	public static String SERVER_PORT;

	public static int TARGET_SERVER_PORT;
	public static int TARGET_SERVER_PORT2;

	public static String TARGET_SERVER_IP;

	public static String DB_DRIVER;

	public static String DB_URL;

	public static String DB_LOGIN;

	public static String DB_PASSWORD;

	public static String TIME_ZONE;

	public static int CLIENT_LANGUAGE;

	public static String CLIENT_LANGUAGE_CODE;

	public static String[] LANGUAGE_CODE_ARRAY = { "UTF8", "EUCKR", "UTF8","BIG5", "SJIS", "GBK" };

	public static boolean HOSTNAME_LOOKUPS;

	public static CopyOnWriteArrayList<ClientThread> CLIENT_THREAD = new CopyOnWriteArrayList<ClientThread>();

	public static ConcurrentMap<Integer , Boolean>PORT_LIST = new ConcurrentHashMap<Integer , Boolean>();

	public static ArrayList<ServerExecutor>SERVER_LIST = new ArrayList<ServerExecutor>();

	/** Configuration files */

	public static final String SERVER_CONFIG_FILE = "./config/server.properties";
	public static void load() {
		loadInfo();
		_log.info("正在讀取遊戲設定");

		// server.properties
		try {
			final Properties serverSettings = new Properties();
			final InputStream is = new FileInputStream(new File(SERVER_CONFIG_FILE));
			serverSettings.load(is);
			is.close();
			TARGET_SERVER_IP = serverSettings.getProperty("TargetserverIp", "127.0.0.1");
			TARGET_SERVER_PORT = Integer.parseInt(serverSettings.getProperty("TargetserverPort", "2000"));
			TARGET_SERVER_PORT2 = Integer.parseInt(serverSettings.getProperty("TargetserverPort2", "2000"));
			SERVER_PORT = serverSettings.getProperty("serverPort", "6262");
			DB_DRIVER = serverSettings.getProperty("Driver","com.mysql.jdbc.Driver");
			DB_URL = serverSettings.getProperty("URL","jdbc:mysql://localhost/l1jdb?useUnicode=true&characterEncoding=sjis");
			DB_LOGIN = serverSettings.getProperty("Login", "root");
			DB_PASSWORD = serverSettings.getProperty("Password", "");
			CLIENT_LANGUAGE = Integer.parseInt(serverSettings.getProperty("ClientLanguage", "4"));
			CLIENT_LANGUAGE_CODE = LANGUAGE_CODE_ARRAY[CLIENT_LANGUAGE];
			TIME_ZONE = serverSettings.getProperty("TimeZone", "GMT+8");//opqlo 修正時區錯誤問題
			HOSTNAME_LOOKUPS = Boolean.parseBoolean(serverSettings.getProperty("HostnameLookups", "false"));
		} catch (final Exception e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
			throw new Error("Failed to Load " + SERVER_CONFIG_FILE + " File.");
		}
		//TODO 伺服器綑綁
	}




	private static void loadInfo() {
		try {
			final Properties logSettings = new Properties();
			final InputStream is = new FileInputStream(new File("revisionlog.ini"));
			logSettings.load(is);
			is.close();
			for (final String element : _systemInfo) {
				System.out.println(element);
			}
		} catch (final Exception e) {
		}
	}

	public static boolean setParameterValue(final String pName, final String pValue) {
		// server.properties
		if (pName.equalsIgnoreCase("serverPort")) {
			SERVER_PORT = pValue;
		} else if(pName.equalsIgnoreCase("TargetserverIp")){
			TARGET_SERVER_IP = pValue;
		} else if(pName.equalsIgnoreCase("TargetserverPort")){
			TARGET_SERVER_PORT = Integer.parseInt(pValue);
		} else if(pName.equalsIgnoreCase("TargetserverPort2")){
			TARGET_SERVER_PORT2 = Integer.parseInt(pValue);
		} else if (pName.equalsIgnoreCase("Driver")) {
			DB_DRIVER = pValue;
		} else if (pName.equalsIgnoreCase("URL")) {
			DB_URL = pValue;
		} else if (pName.equalsIgnoreCase("Login")) {
			DB_LOGIN = pValue;
		} else if (pName.equalsIgnoreCase("Password")) {
			DB_PASSWORD = pValue;
		} else if (pName.equalsIgnoreCase("ClientLanguage")) {
			CLIENT_LANGUAGE = Integer.parseInt(pValue);
		} else if (pName.equalsIgnoreCase("TimeZone")) {
			TIME_ZONE = pValue;
		}
		//TODO 調整能力值上限 by 阿傑
		else {
			return false;
		}
		return true;
	}

	private Config() {
	}
}